﻿using System;
using GammaJul.ReSharper.GammaXaml.Highlightings.Xaml;
using JetBrains.Annotations;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Feature.Services.Bulbs;
using JetBrains.ReSharper.Intentions.Extensibility;
using JetBrains.ReSharper.Intentions.Extensibility.Menu;
using JetBrains.Util;

namespace GammaJul.ReSharper.GammaXaml.QuickFixes.Xaml {

	/// <summary>
	/// A quick fix that presents a group of <see cref="UseSpanItem"/>, used to change the values of a row span or column span.
	/// </summary>
	[QuickFix]
	public class UseSpanFix : IQuickFix {
		private readonly GridSpanValueOutOfRangeHighlighting _highlighting;
		
		public void CreateBulbItems(BulbMenu menu, Severity severity) {
			int maxSpan = _highlighting.GridDefinitionSizes.Count - _highlighting.DefinitionValue;
			BulbGroup group = menu.GetOrCreateGroup(AnchorsForBulbMenuGroups.QuickFixesAnchor);

			for (int i = 1; i <= maxSpan; ++i)
				group.AddBulbAction(Anchor.DefaultAnchor, new UseSpanItem(_highlighting, i), UnnamedThemedIcons.Grid.Id);
		}

		public bool IsAvailable(IUserDataHolder cache) {
			return _highlighting.IsValid();
		}

		public UseSpanFix([NotNull] GridSpanValueOutOfRangeHighlighting highlighting) {
			if (highlighting == null)
				throw new ArgumentNullException("highlighting");
			_highlighting = highlighting;
		}
	}

}